<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /**
     * 延迟输出的异步函数，用Promise进行包装
     *  duration: 延迟的时间
     *  n: 延迟输出的数
     */
    function delay (duration, n) {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve(n)
        }, duration)
      })
    }

    // Promise的链式调用消除回调地狱
    delay(2000, 1)
      .then((n1) => {
        console.log(n1)
        return delay(1000, 2)
      })
      .then((n2) => {
        console.log(n2)
        return delay(1000, 3)
      })
      .then((n3) => {
        console.log(n3)
      })
  </script>
</body>

</html>